package com.eeepay.eeepay_v2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

 /**
  * 描述：单行TextView，容纳不下就左移
  * 作者：zhuangzeqin
  * 时间: 2017/6/7-9:06
  * 邮箱：zzq@eeepay.cn
  */
public class FloatableTextView extends TextView implements View.OnClickListener {

    public boolean mIsFloating = false; //是否开始滚动
    private float mSpeed = 0.5f;
    private float mStep = 0f;
    private String mStr = ""; //文本内容
    private float mTextLength = 0f; //文本长度
    private float mViewWidth = 0f;
    private float mY = 0f; //文字的纵坐标


    public FloatableTextView(Context context) {
        super(context);
        initView();
    }

    public FloatableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public FloatableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }


    private void initView() {
        setOnClickListener(this);
    }

    public void init(String str, float speed, float width) {
        setText(str);
        mSpeed = speed;
        mStr = getText().toString();
        mTextLength = getPaint().measureText(mStr);
        mViewWidth = width;
        mStep = mTextLength + mViewWidth;
        mY = getTextSize() + getPaddingTop();
        getPaint().setColor(0xff0000ff);
    }

    @Override
    public void onClick(View v) {
        if (mIsFloating)
            stopFloating();
        else
            startFloating();
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawText(mStr, 0, mStr.length(), mViewWidth + mTextLength - mStep, mY, getPaint());
        if (!mIsFloating)
            return;
        mStep += mSpeed;
        if (mStep > mViewWidth + mTextLength * 2)
            mStep = mTextLength;
        invalidate();
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SavedState savedState = (SavedState) state;
        super.onRestoreInstanceState(savedState.getSuperState());

        mStep = savedState.mStep;
        mIsFloating = savedState.mIsFloating;
    }

    @Override
    public Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState savedState = new SavedState(superState);
        savedState.mStep = mStep;
        savedState.mIsFloating = mIsFloating;
        return savedState;
    }

    public void setSpeed(float speed) {
        mSpeed = speed;
    }

    public void startFloating() {
        mIsFloating = mTextLength > mViewWidth;
        // mIsFloating = true;
        invalidate();
    }

    public void stopFloating() {
        mIsFloating = false;
        invalidate();
    }

    public static class SavedState extends BaseSavedState {
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {

            @Override
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }

        };
        public boolean mIsFloating = false;
        public float mStep = 0.0f;

        private SavedState(Parcel in) {
            super(in);
            boolean[] b = new boolean[1];
            in.readBooleanArray(b);
            mIsFloating = b[0];
            mStep = in.readFloat();
        }

        SavedState(Parcelable superState) {
            super(superState);
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeBooleanArray(new boolean[] {
                    mIsFloating
            });
            out.writeFloat(mStep);
        }
    }

}